package service

import (
	"context"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
	"wechat_1004/global"
	"wechat_1004/internal/model"
	"wechat_1004/internal/params"
)

func AddMoment(c *gin.Context) {
	var req params.AddMomentForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var moment model.Moment

	moment = model.Moment{
		UserId:       moment.UserId,
		Title:        moment.Title,
		ContentImage: moment.ContentImage,
		ContentText:  moment.ContentText,
	}

	err := moment.AddMoment()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "发布失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "发布成功",
		"data":    moment.Id,
	})

}

func FindMoment(c *gin.Context) {
	var req params.FindMomentForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
			"data":    "",
		})
		return
	}

	var moment model.Moment
	moment.UserId = req.UserId
	findMoment, err := moment.FindMoment()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "查看失败",
			"data":    err.Error(),
		})
		return
	}

	global.RDB.Set(context.Background(), "findMoment", findMoment, time.Minute*30)
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "查看成功",
		"data":    findMoment,
	})

}
